package net.xyz.dropitem;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class DropUtil {
	private static ArrayList<Material> TreeMaterials=new ArrayList<>();
	private static ArrayList<Material> MineMaterials=new ArrayList<>();
	private  static ArrayList<ItemStack> TreeitemStacks=new ArrayList<>();
	private  static ArrayList<ItemStack> MineitemStacks=new ArrayList<>();
	private static ItemStack isStack=null;
	
	public static ArrayList<Material> getTreeTypeMaterial(){
		return TreeMaterials;
	}
	public static ArrayList<Material> getMineTypeMaterial(){
		return MineMaterials;
	}
	public static void setTreeTypeMaterials(String m){
		/*
		 * 设置砍树Material
		 */
		if(m!=null){
		List<String> str=DropItem.getCg().getStringList("TreeType");
		String string=listToString(str);
		String[] str2=string.split(",");
		for(int i = 0;i<str2.length;i++){
			Material iMaterial=Material.getMaterial(str2[i]);
			TreeMaterials.add(iMaterial);
		}
		}else{
			MineMaterials.clear();
		}
	}
	public static void setMineTypeMaterials(String m){
		/*
		 * 设置挖矿Material
		 */
		if(m!=null){
		List<String> str=DropItem.getCg().getStringList("MineType");
		String string=listToString(str);
		String[] str2=string.split(",");
		for(int i = 0;i<str2.length;i++){
			Material iMaterial=Material.getMaterial(str2[i]);
			MineMaterials.add(iMaterial);
		}
		}else{
			MineMaterials.clear();
		}
	}
	public static String listToString(List<String> list){
		/*
		 * list转string
		 */
	      if(list==null){
	      return null;
	   }
	   StringBuilder result = new StringBuilder();
	   boolean first = true;
	   for(String string :list) {
	      if(first) {
	         first=false;
	      }else{
	         result.append(",");
	      }
	      result.append(string);
	   }
	   return result.toString();
	}
	@SuppressWarnings("deprecation")
	public static ItemStack getDropTree(){
		/*
		 * 获得砍树掉落物
		 */
		Random random=new Random();
		List<String> str=DropItem.getCg().getStringList("DropTree");
		String string=listToString(str);
		String[] str2=string.split(",");
		for(int i=0;i<str2.length;i++){
			String[] str3=str2[i].split(":");
			Material material=Material.getMaterial(str3[0]);
			byte b=(byte)Integer.valueOf(str3[1]).intValue();
			double d=Double.valueOf(str3[2]);
			if(random.nextInt(100)<=(d*100)){
				TreeitemStacks.add(new ItemStack(material, 1, material.getMaxDurability(), b));
			}
		}
		if(TreeitemStacks.size()>0&&random.nextInt(100)<=(int)((DropItem.getCg().getDouble("Chance"))*100)){
			int myRand = random.nextInt(TreeitemStacks.size());
            return TreeitemStacks.get(myRand);
		}
		return isStack;
	}
	@SuppressWarnings("deprecation")
	public static ItemStack getDropMine(){
		/*
		 * 获得挖矿掉落物
		 */
		Random random=new Random();
		List<String> str=DropItem.getCg().getStringList("DropMine");
		String string=listToString(str);
		String[] str2=string.split(",");
		for(int i=0;i<str2.length;i++){
			String[] str3=str2[i].split(":");
			Material material=Material.getMaterial(str3[0]);
			byte b=(byte)Integer.valueOf(str3[1]).intValue();
			double d=Double.valueOf(str3[2]);
			if(random.nextInt(100)<=(d*100)){
				MineitemStacks.add(new ItemStack(material, 1, material.getMaxDurability(), b));
			}
		}
		if(MineitemStacks.size()>0&&random.nextInt(100)<=(int)((DropItem.getCg().getDouble("Chance"))*100)){
			int myRand = random.nextInt(MineitemStacks.size());
            return MineitemStacks.get(myRand);
		}
		return isStack;
	}
	public static void clearTreeMaterials(){
		TreeitemStacks.clear();
	}
	public static void clearMineMaterials(){
		MineitemStacks.clear();
	}
}
